package com.wkbb.sports.utils;

import java.util.HashMap;
import java.util.Map;

import com.wkbb.common.enums.SexTypeEnum;
import com.wkbb.common.utils.StringUtil;
import com.wkbb.sports.enums.SportsUnitEnum;

public class SportsUtils {

	public static double translate(String strUnit, String achievement) {
		int unit = 0;
		if(strUnit != null && !"".equals(strUnit)) {
			try{
				unit = Integer.parseInt(strUnit.trim());
			}catch(Throwable e) {
				logerror("illegal unit:" + strUnit);
				throw e;
			}
		}
		if(SportsUnitEnum.UNIT_MINUTE.getValue() == unit) {
			if(achievement.endsWith("\"")) {
				achievement = achievement.substring(0, achievement.length() - 1);
			}
			int pos = achievement.indexOf('\'');
			if(pos > 0) {
				try{
					return Integer.parseInt(achievement.substring(0, pos)) * 60 +  Integer.parseInt(achievement.substring(pos + 1));
				}catch(Throwable e) {
					logerror("unit:UNIT_MINUTE, illegal achievement:" + achievement);
					throw e;
				}
			}
		}
		try{
			return Double.parseDouble(achievement);
		}catch(Throwable e) {
			logerror("unit:" + unit + ", illegal achievement:" + achievement);
			throw e;
		}
	}

	public static String getSex(String gender) {
		//性别1男2女
		if (StringUtil.isEmpty(gender)) {
			return null;
		}

		if (gender.equals(SexTypeEnum.MAN.getValue().toString())) {
			return SexTypeEnum.MAN.getDesc();
		} else if (gender.equals(SexTypeEnum.WOMAN.getValue().toString())) {
			return SexTypeEnum.WOMAN.getDesc();
		} else {
			return SexTypeEnum.UNKNOWN.getDesc();
		}
	}

	public static Integer getSexValue(String gender) {
		//性别1男2女
		if (StringUtil.isEmpty(gender)) {
			return null;
		}

		if (gender.equals(SexTypeEnum.MAN.getDesc().toString())) {
			return SexTypeEnum.MAN.getValue();
		} else if (gender.equals(SexTypeEnum.WOMAN.getDesc().toString())) {
			return SexTypeEnum.WOMAN.getValue();
		} else {
			return SexTypeEnum.UNKNOWN.getValue();
		}
	}

	private static void logerror(String msg) {
		System.out.println("ERROR " + msg);
		//log.error(msg);
	}
	
	
	public static int covert(String content){
        int number=0;
        String [] HighLetter = {"A","B","C","D","E","F"};
        Map<String,Integer> map = new HashMap<>();
        for(int i = 0;i <= 9;i++){
            map.put(i+"",i);
        }
        for(int j= 10;j<HighLetter.length+10;j++){
            map.put(HighLetter[j-10],j);
        }
        String[]str = new String[content.length()];
        for(int i = 0; i < str.length; i++){
            str[i] = content.substring(i,i+1);
        }
        for(int i = 0; i < str.length; i++){
            number += map.get(str[i])*Math.pow(16,str.length-1-i);
        }
        return number;
    }
	
	
	public static void main(String[] args){
		//SportsUtils   s = new SportsUtils();
		//System.out.print(s.covert("0169"));
		String newdata = "02010603030D1807FF04FF024501590E09434C3833312D30323033313838";
		String xl = newdata.substring(28, 30);
		int xldata = Integer.parseInt(xl,16);
		System.out.println("xl:"+xl+"\txldata:"+xldata);

//		String a="30";
//
//		String b="03";
//
//		System.out.print(Integer.parseInt(a)==Integer.parseInt(b));
	}

}
